package com.tian.photopicker.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.RequestManager;
import com.bumptech.glide.request.RequestOptions;
import com.tian.photopicker.R;
import com.tian.photopicker.bean.PhotoDirectory;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author tian
 * @date 2017/11/6
 */

public class PopupDirectoryAdapter extends BaseAdapter{
    private RequestManager glide;
    private List<PhotoDirectory> directories = new ArrayList<>();

    public PopupDirectoryAdapter(RequestManager glide, List<PhotoDirectory> directories) {
        this.glide = glide;
        this.directories = directories;
    }

    @Override
    public int getCount() {
        return directories.size();
    }

    @Override
    public Object getItem(int position) {
        return directories.get(position);
    }

    @Override
    public long getItemId(int position) {
        return directories.get(position).hashCode();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DirectoryHolder holder;
        if (convertView == null) {
            convertView = View.inflate(parent.getContext(),R.layout.item_directories_layout,null);
            holder = new DirectoryHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (DirectoryHolder) convertView.getTag();
        }
        holder.bindData(directories.get(position));
        return convertView;
    }
    private class DirectoryHolder {
        public ImageView ivCover;
        public TextView tvName;
        public TextView tvCount;

        public DirectoryHolder(View rootView) {
            ivCover = (ImageView) rootView.findViewById(R.id.iv_cover);
            tvName  = (TextView) rootView.findViewById(R.id.tv_dir_name);
            tvCount = (TextView) rootView.findViewById(R.id.tv_dir_count);
        }

        public void bindData(PhotoDirectory directory) {
            final RequestOptions options = new RequestOptions();
            options.dontAnimate()
                    .dontTransform()
                    .override(800, 800)
                    .placeholder(R.drawable.icon_place_holder)
                    .error(R.drawable.icon_place_holder);
            glide.setDefaultRequestOptions(options)
                    .load(directory.getCoverPath())
                    .thumbnail(0.1f)
                    .into(ivCover);
            tvName.setText(directory.getName());
            tvCount.setText(directory.getPhotos().size() + "张");
        }
    }

}
